package vn.cuhanh.utils;

import java.io.InputStream;

import vn.cuhanh.R;
import vn.cuhanh.customview.GifWebView;
import vn.cuhanh.customview.MyWebViewClient;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;

public class ViewHelper {

	public static void initLoadingFrame(Context ctx, View v) {
		InputStream stream = null;
		v.setBackgroundColor(0xFFF0F0F0);
        try {
        	DisplayMetrics metrics = new DisplayMetrics();
        	((Activity)ctx).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        	switch(metrics.densityDpi){
    	     case DisplayMetrics.DENSITY_LOW:
    	    	 stream = ctx.getAssets().open("loading_120.gif");
    	         break;
    	     case DisplayMetrics.DENSITY_MEDIUM:
    	    	 stream = ctx.getAssets().open("loading_160.gif");
    	                 break;
    	     case DisplayMetrics.DENSITY_HIGH:
    	    	 stream = ctx.getAssets().open("loading_240.gif");
                 break;
             default:
            	 stream = ctx.getAssets().open("loading_320.gif");
            	 break;
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        WebView loadingWebView = (WebView) v.findViewById(R.id.loadingWebView);
		loadingWebView.setWebViewClient(new MyWebViewClient());
		loadingWebView.getSettings().setJavaScriptEnabled(true);
		loadingWebView.setOnTouchListener(new View.OnTouchListener() {

		    public boolean onTouch(View v, MotionEvent event) {
		      return (event.getAction() == MotionEvent.ACTION_MOVE);
		    }
		  });
//		loadingWebView.setHorizontalScrollBarEnabled(false);
//		loadingWebView.setVerticalScrollBarEnabled(false);
        GifWebView view = new GifWebView(ctx, stream);
        view.setBackgroundColor(0xFFF0F0F0);
        loadingWebView.addView(view);
	}
}
